package com.atguigu.day04;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class Flink04_Transfrom_Connect {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource999 = env.socketTextStream("localhost", 9999);

        DataStreamSource<String> streamSource888 = env.socketTextStream("localhost", 8888);

        //TODO 使用Connect合并两条流
        //注意：connect 只能链接两条流 两条流中的类型可以不一致
        ConnectedStreams<String, String> connectedStreams = streamSource999.connect(streamSource888);
        SingleOutputStreamOperator<String> result = connectedStreams.map(new CoMapFunction<String, String, String>() {
            @Override
            public String map1(String value) throws Exception {
                return value + "：第一条流";
            }

            @Override
            public String map2(String value) throws Exception {
                return value + ":第二条流";
            }
        });

        result.print();

        env.execute();

    }
}
